package com.fh.shop.api.member.controller;

import com.fh.shop.api.annotation.NeedLogin;
import com.fh.shop.api.common.ServerResponse;
import com.fh.shop.api.common.SystemConst;
import com.fh.shop.api.member.biz.IMemberService;
import com.fh.shop.api.member.param.EditPasswordParam;
import com.fh.shop.api.member.param.LoginParam;
import com.fh.shop.api.member.param.MemberParam;
import com.fh.shop.api.member.vo.MemberVo;
import com.fh.shop.api.util.KeyUtil;
import com.fh.shop.api.util.RedisUtil;
import io.swagger.annotations.Api;;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/api/members")
@CrossOrigin
@Api(tags = "会员接口")
public class MemberController {

    @Resource(name = "memberService")
    private IMemberService memberService;
    @Resource
    private HttpServletRequest request;
    @Resource
    private HttpServletResponse response;
    @Value("${mail.active.member.success}")
    private String mailSuccess;
    @Value("${mail.active.member.error}")
    private String mailError;

    //注册
    @PostMapping
    public ServerResponse reg(@RequestBody MemberParam memberParam){

        return memberService.reg(memberParam);
    }

    //登录
    @PostMapping("/login")
    public ServerResponse login(LoginParam loginParam){
        return memberService.login(loginParam);
    }

    //获取会员信息
    @GetMapping("/findMember")
    @NeedLogin
    public ServerResponse findMember(){
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConst.CURR_MEMBER);
        String nickName = memberVo.getNickName();
        return ServerResponse.success(nickName);
    }

    //注销
    @PostMapping("/logout")
    @NeedLogin
    public ServerResponse logout(){
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConst.CURR_MEMBER);
        Long id = memberVo.getId();
        //清除数据
        RedisUtil.delete(KeyUtil.buildMemberKey(id));
        return ServerResponse.success();
    }

    //修改密码
    @PostMapping("/editPassword")
    @NeedLogin
    public ServerResponse editPassword(EditPasswordParam passwordParam){
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConst.CURR_MEMBER);
        Long id = memberVo.getId();
        passwordParam.setMemberId(id);
        return memberService.updatePassword(passwordParam);
    }

    //发送邮件 【根据id激活】
    @GetMapping("active/{x}")
    public void activeMember(@PathVariable("x") String uuid) throws IOException {
        int status = memberService.activeMember(uuid);
        if (status == SystemConst.MEMBER_ACTIVE_OK){
            response.sendRedirect(mailSuccess);
        }else {
            response.sendRedirect(mailError);
        }
    }

    // 再次 -- 激活会员 【路径传参,不需要做非空判断】
    @PostMapping("/sendActiveMail/{mail}")
    public ServerResponse sendActiveMail(@PathVariable String mail){
        return memberService.sendActiveMail(mail);
    }

    // 再次发送邮件 【根据邮箱激活】[方法是多种多样的,效果一样]
    @GetMapping("mail/active/{x}")
    public void activeMailMember(@PathVariable("x") String uuid) throws IOException {
        boolean success = memberService.activeMailMember(uuid);
        if (success){
            response.sendRedirect(mailSuccess);
        }else {
            response.sendRedirect(mailError);
        }
    }
}
